package com.neusoft.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.po.CommonResult;
import com.neusoft.po.Food;
import com.neusoft.service.FoodService;

@RestController
@RequestMapping("/Food")
@RefreshScope //开启动态刷新
public class FoodController {

	@Autowired
	private FoodService foodService;

	@GetMapping("/FoodList")
	public CommonResult<List> listFoodByBusinessId(@RequestParam("businessId") Integer businessId) throws Exception {
		List<Food> list = foodService.listFoodByBusinessId(businessId);
        return new CommonResult<List>(200, "success", list);
	}

	// get food by id
	@GetMapping("/FoodId")
	public CommonResult<Food> getFoodById(Food food) throws Exception {
		return new CommonResult<Food>(200, "success", foodService.getFoodById(food.getFoodId()));
	}

	// save food
	@PostMapping("/FoodId")
	public CommonResult<Integer> saveFood(Food food) throws Exception {
		return new CommonResult<Integer>(200, "success", foodService.saveFood(food));
	}

	// update food
	@PutMapping("/FoodId")
	public CommonResult<Integer> updateFood(Food food) throws Exception {
		return new CommonResult<Integer>(200, "success", foodService.updateFood(food));
	}

	// remove food
	@DeleteMapping("/FoodId")
	public CommonResult<Integer> removeFood(Food food) throws Exception {
		return new CommonResult<Integer>(200, "success", foodService.removeFood(food.getFoodId()));
	}

	// update count
	@PutMapping("/FoodCount")
	public CommonResult<Integer> updateFoodCount(Integer foodCount, Integer foodId) {
		return new CommonResult<Integer>(200, "success", foodService.updateFoodCount(foodCount, foodId));
	}
}